<html>
<div id="myheader">
<?php
//error_reporting(0);
session_start();
error_reporting(E_ALL);

$user_id = $_SESSION['user_id'];
$user_name = $_SESSION['user_name'];

if (!isset($user_id ) )
{
echo "<h1> Please login</h1>";
echo "<br />";
header("Location: /OSPMS/index.php");
echo '<script type="text/javascript">';
echo 'alert("Please Login!")';
echo '</script>';
}
?>
</div>
<link rel="stylesheet" href="style/ospms.css" type="text/css">
  <script language="JavaScript" type="text/javascript" src="js/jquery.js"></script>
<title>OSPMS</title>
<head>

<style type="text/css" media="all">

#listDiv{width:500px; overflow:auto;}
#updateDiv{width:300px; vertical-align:top;}
#middleDiv{ height:500px; width:2px; background:darkgray;}
</style>
</head>



<?php

include "include/dbConfig.php";

try {
    $conn = new PDO("mysql:host=$dbservername;dbname=$dbname", $dbusername, $dbpassword);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

	$query = 'SELECT * FROM user';
	$userList = $conn->query($query);


$username = $_SESSION['user_name'];
$user_id = $_SESSION['user_id'];


    $query = "SELECT user_id,user_name,first_name,last_name,email,website,password  FROM user where user_name = '" .$user_name. "'";
    $stmt = $conn->prepare($query);
    $stmt->execute();
    // set the resulting array to associative
    $result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
$array_user = $stmt->fetchAll();
    $dsn = null;

foreach ($array_user as $key => $value) {
  $record = $value;
 
  $user_name = $record['user_name'];
  $user_id = $record['user_id'];
  $first_name = $record['first_name'];
  $last_name = $record['last_name'];
  $website = $record['website'];
  $email = $record['email'];
}

} // try
catch(PDOException $e)
{
    echo "Error: " . $e->getMessage();
} // try - catch

$conn = null;
?> 


<form method="get" action="update_user_submit.php">   <!-- default: get; all post most be specified -->
<div id="subMain">

<table><tr>
<td valign="top">
<div id="listDiv">
<?php

	$num=0;
echo "<table cellpadding='5' cellspacing='5' class='DataTable'>";
echo "<tr><th>Account</th><th>User Name</th><th>Email</th><th>Edit</th></tr>";
foreach($userList as $key => $oneUser)
	{
		$num++;
		if($num%2==1)
			echo "<tr class='tableData'>";
		else 
			echo "<tr class='tableData2'>";
	echo "<td>". $oneUser['user_name']."</td>";
	echo "<td>". $oneUser['first_name']."&nbsp;". $oneUser['last_name']."</td>";

	echo "<td>". $oneUser['website']."</td>";
	echo "<td><a href='javascript:void()' onclick=\"clickUpdate('".$oneUser['user_id']."','".$oneUser['user_name']."','".$oneUser['first_name']."','". $oneUser['last_name']."','". $oneUser['email']."','". $oneUser['website']."')\">Edit</a></td>";
		echo "</tr>";
		
	}
	echo "</table>"

?>
</div>
</td>
<td valign="middle">
<div id="middleDiv"></div>
</td>
<td valign="top">
<div id="updateDiv">
<table class="editTable" >
    
	<tr>
      <td class="lefttd">Account:</td>
      <td class="righttd"><input type="text" name="userName" value="<?php echo $user_name ?>"  class="text"/></td>
    </tr>
    <!--<tr>
      <td class="lefttd">Current Password:</td>
      <td class="righttd"><input type="password" name="userPwd" class="text"/></td>
    </tr>-->
    <tr>
      <td class="lefttd">New Password:</td>
      <td class="righttd"><input type="password" name="userPwd1" class="text"/></td>
    </tr>
    <tr>
      <td class="lefttd">Repeat Password:</td>
      <td class="righttd"><input type="password" name="userPwd2" class="text"/></td>
    </tr>
    <tr>
      <td class="lefttd">First Name</td>
      <td class="righttd"><input type="text" name="userFirstName" value="<?php echo $first_name ?>" class="text"/></td>
    </tr>
    <tr>
      <td class="lefttd">Last Name</td>
      <td class="righttd"><input type="text" name="userLastName" value="<?php echo $last_name?>" class="text"/></td>
    </tr>
    <tr>
      <td class="lefttd">Email:</td>
      <td class="righttd"><input type="text" name="eMail" value="<?php echo $email ?>" class="text"/></td>
    </tr>
    <tr>
      <td class="lefttd">Web Site:</td>
      <td class="righttd"><input type="text" name="website" value="<?php echo $website ?>" class="text"/></td>
    </tr>
    <tr>
	
      <td class="righttd" colspan="2" >
	<input type="hidden" name="userID" value="">
        <input type="submit" value="Update/Add User Profile" class="ActionButton" name="cmdLogin" id="cmdLogin"/>
		 <input type="submit" value="Add User Profile" class="ActionButton" name="cmdLoginAdd" id="cmdLoginAdd" style="display:none"/>
		<!--<input type="checkbox" name="IsNew" onclick="clickNew()">New User-->
      </td>
    </tr>
</table>
</div>
</td>
</tr></table>
</div>
</form>

<script>
function clickUpdate(id,username,fname,lname,email,website)
	{
		$("[name='userName']").val(username);
		$("[name='userFirstName']").val(fname);
		$("[name='userLastName']").val(lname);
		$("[name='eMail']").val(email);
		$("[name='website']").val(website);
		$("[name='userID']").val(id);
		$("[name='cmdLogin']").css("display","");
		$("[name='cmdLoginAdd']").css("display","none");
	}
	function clickNew()
	{
	return;
		$("[name='cmdLogin']").css("display","none");
		$("[name='cmdLoginAdd']").css("display","");
		$("[name='userID']").val("");
	}
	
	$("#listDiv").css("height",$(document.body).height()-10);
</script>

